matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

var rotate = function (matrix) {
    const n = matrix.length;
    const matrix_new = new Array(n).fill(0).map(() => new Array(n).fill(0));
    for (let i = 0; i < n; i++) {
        for (let j = 0; j < n; j++) {
            matrix_new[j][n - i - 1] = matrix[i][j];
        }
    }
    for (let i = 0; i < n; i++) {
        for (let j = 0; j < n; j++) {
            matrix[i][j] = matrix_new[i][j];
        }
    }
};

var rotate = function (matrix) {
    // 顺时针旋转90°，交换行和列的元素，再反转每一行
    let m = matrix.length, n = matrix[0].length;

    for (let i = 0; i < m; i++) {
        for (let j = i + 1; j < n; j++) {
            [matrix[i][j], matrix[j][i]] = [matrix[j][i], matrix[i][j]];
        }
    }

    for (let i = 0; i < m; i++) {
        matrix[i].reverse();
    }
}
